package com.xingkong.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.List;
import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xingkong.common.annotation.Log;
import com.xingkong.common.enums.BusinessType;
import com.xingkong.product.domain.ProductPrice;
import com.xingkong.product.service.IProductPriceService;
import com.xingkong.common.core.controller.BaseController;
import com.xingkong.common.core.text.Convert;
import com.xingkong.common.core.domain.AjaxResult;
import com.xingkong.common.utils.poi.ExcelUtil;
import com.xingkong.common.core.page.TableDataInfo;

/**
 * 商品价格Controller
 *
 * @author yiyue
 * @date 2020-03-15
 */
@Controller
@RequestMapping("/product/price")
public class ProductPriceController extends BaseController {
    private String prefix = "product/price";

    @Autowired
    private IProductPriceService productPriceService;

    @RequiresPermissions("product:price:view")
    @GetMapping()
    public String price() {
        return prefix + "/price";
    }

    /**
     * 查询商品价格列表
     */
    @RequiresPermissions("product:price:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductPrice productPrice) {
        startPage();
        return getDataTable(productPriceService.list(new QueryWrapper<ProductPrice>(productPrice)));
    }

    /**
     * 导出商品价格列表
     */
    @RequiresPermissions("product:price:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductPrice productPrice) {
        List<ProductPrice> list = productPriceService.list(new QueryWrapper<>());
        ExcelUtil<ProductPrice> util = new ExcelUtil<ProductPrice>(ProductPrice.class);
        return util.exportExcel(list, "price");
    }

    /**
     * 新增商品价格
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存商品价格
     */
    @RequiresPermissions("product:price:add")
    @Log(title = "商品价格", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductPrice productPrice) {
        return toAjax(productPriceService.save(productPrice));
    }

    /**
     * 修改商品价格
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        ProductPrice productPrice = productPriceService.getById(id);
        mmap.put("productPrice", productPrice);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品价格
     */
    @RequiresPermissions("product:price:edit")
    @Log(title = "商品价格", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductPrice productPrice) {
        return toAjax(productPriceService.updateById(productPrice));
    }

    /**
     * 删除商品价格
     */
    @RequiresPermissions("product:price:remove")
    @Log(title = "商品价格", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(productPriceService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
